package com.kdtech.analyse.news.ulinix;

import com.kdtech.analyse.AnalyseNews;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;

/**
 * Hotanuqur
 * @author Persh
 *
 */
public class LeglekNewsAnalyse implements AnalyseNews {
	
	private static final String[] regex={
		"http://www.leglek.com/news.php\\?id=[0-9]*"
	};
	
	
	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}		
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();		
				
		news.setUrl(url);		
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		title =doc.select("div.newsTitle").text();
		content=HtmlCleaner.getContentHtml(url,doc.select("div.newsText"));
		String year,month, day;
		Elements dateList = doc.select("div.newsList-dt");
		if(dateList.size() > 0){
			String dateStr = dateList.get(0).text();
			year = dateStr.substring(6);
			month = dateStr.substring(3,5);
			day = dateStr.substring(0,2);
			date = DateUtils.matchDate(year + month + day);
		}

		if(StringUtils.isBlank(title)){
		}
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);	
		
		return news;
	}
	
	

	

	
}
